﻿#region License, Terms and Conditions
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
#endregion
using System;
using System.Web.Mvc;
using raya.ActionFilters;
using raya.Models.people;

namespace raya.Controllers
{
    //[HandleError]
    public class homeController : baseController
    {
        public ActionResult Index()
        {
            Response.AppendHeader(
                "X-XRDS-Location",
                new Uri(Request.Url, Response.ApplyAppPathModifier("~/Home/xrds")).AbsoluteUri);
            if (Session["id"] != null)
            {
                Profile thisProfile = null;
                if (Session["id"] != null)
                {
                    thisProfile = GetProfile(Session["id"].ToString());
                }
                return View("Home", thisProfile);
            }
            var data = people.getRecentMembers(18);
            return View("Index",data);
        }

        public ActionResult Xrds()
        {
            return View("Xrds");
        }

        public ActionResult removefriend(string id)
        {
            string message = "Friend removed";
            var people = new people();
            if (Session["id"] != null)
            {
                people.RemoveFriend(Session["id"].ToString(), id);
            }
            else
            {
                message = "Could not remove friend request, invalid friend id";
            }
            handleErrors(message);
            people.Dispose();
            return Redirect("/");
        }

        public ActionResult addfriend(string id)
        {
            string message;
            var people = new people();
            if (Session["id"] != null)
            {
#if AZURE
                bool success = people.AddFriendRequest(Session["id"].ToString(), id);
#else
                bool success = people.AddFriendRequest((int) Session["id"], int.Parse(id));
#endif
                if (success)
                {
#if AZURE
                    var info = people.GetPersonInfo(id);
#else
                    var info = people.GetPersonInfo(int.Parse(id));
#endif
                    message = "Sent friend request to " + info.name;
                }
                else
                {
                    message = "Could not send friend request, request already pending";
                }
            }
            else
            {
                message = "Could not send friend request, invalid friend id";
            }
            handleErrors(message);
            people.Dispose();
            return Redirect("/home");
        }

        public ActionResult acceptfriend(string id)
        {
            var people = new people();
            try
            {
                string message = "Friend request accepted";
                if (Session["id"] != null)
                {
                    people.AcceptFriendRequest(Session["id"].ToString(), id);
                }
                else
                {
                    message = "Could not accept friend request, your session has expired";
                }
                handleErrors(message);
            }
            catch (Exception ex)
            {
                handleErrors(ex.Message);
            }
            people.Dispose();
            return Redirect("/home");
        }

        public ActionResult acceptrecommend(string id)
        {
            var people = new people();
            try
            {
                string message = "Friend added successfully";

                if (Session["id"] != null)
                {
                    people.AddFriend(Session["id"].ToString(), id);
                }
                else
                {
                    message = "Could not add friend, your session has expired";
                }
                handleErrors(message);
            }
            catch (Exception ex)
            {
                handleErrors(ex.Message);
            }
            people.Dispose();
            return Redirect("/home");
        }

        public ActionResult rejectfriend(string id)
        {
            
            var people = new people();
            try
            {
                string message;
                if (Session["id"] != null)
                {
                    message = people.RejectFriendRequest(Session["id"].ToString(), id) ? "Friend request removed" : "Could not remove friend request";
                }
                else
                {
                    message = "Could not remove friend request, invalid friend id";
                }
                handleErrors(message);
            }
            catch (Exception ex)
            {
                handleErrors(ex.Message);
            }
            people.Dispose();
            return Redirect("/");
        }
    }
}